
package com.life.diary.ui;

import com.life.diary.R;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private ViewPager mPager;
    private LinearLayout mDiaryLayout;
    private LinearLayout mSettingLayout;
    private ArrayList<View> mViews = new ArrayList<View>(); 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        setupViews();
    }
    
    private void setupViews() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new DiaryFragment());
        fragments.add(new DiarySettingFragment());
        DiaryAdapter adapter = new DiaryAdapter(getSupportFragmentManager(), fragments);
        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(adapter);
        mPager.setOnPageChangeListener(mOnPageChangeListener);
        mDiaryLayout = (LinearLayout)findViewById(R.id.book);
        mDiaryLayout.setOnTouchListener(mClickListener);
        mViews.add(mDiaryLayout);
        mSettingLayout = (LinearLayout)findViewById(R.id.setting);
        mSettingLayout.setOnTouchListener(mClickListener);
        mViews.add(mSettingLayout);
        mPager.setCurrentItem(0);
        setSelectedTab(0);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    class DiaryAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragments = null;

        public DiaryAdapter(FragmentManager fm, List<Fragment> fragments) {
            // TODO Auto-generated constructor stub
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return (null == fragments)?null:fragments.get(position);
        }
        
        @Override
        public int getCount() {
            return (null == fragments)?-1:fragments.size(); 
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
            // TODO Auto-generated method stub
        }
    }
    
    private OnPageChangeListener  mOnPageChangeListener = new OnPageChangeListener() {
        @Override
        public void onPageSelected(int viewId) {
            setSelectedTab(viewId);
        }
        
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
        
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    };

    OnTouchListener mClickListener = new OnTouchListener() {
        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int index = mViews.indexOf(v);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPager.setCurrentItem(index);
                    break;
                case MotionEvent.ACTION_UP:
                    setSelectedTab(index);
                    break;
                default:
                    break;
            }
            return false;
        }
    };
    
    private void setSelectedTab(int index) {
        LinearLayout layout;
        for(int i=0;i<mViews.size();i++){
            layout = (LinearLayout)mViews.get(i);
            if(i == index){
                layout.setPressed(true);
            }else{
                if(layout.isPressed()){
                    layout.setPressed(false);
                }
            }
        }
    } 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
